package com.lipo.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class MyHandlerException implements HandlerExceptionResolver{

	private Logger logger = Logger.getLogger(MyHandlerException.class);
	
	@Override
	public ModelAndView resolveException(HttpServletRequest req,
			HttpServletResponse resonse, Object arg2, Exception ex) {
		logger.debug("error", ex);
		
		try {
			PrintWriter out = resonse.getWriter();
			JSONObject json = new JSONObject();
			json.put("code", -1);
			json.put("data", "{}");
			json.put("msg", "异常信息。");
			json.put("ex", ex.toString());
			out.print(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ex.printStackTrace();
		
		return new ModelAndView();
	}

}
